package com.music.utils;

import java.util.*;

public class MapSortUtil {
    public static HashMap<String, Integer> sortByValue(HashMap<String, Integer> hm)
    {
        // HashMap的entry放到List中
        List<Map.Entry<String, Integer> > list = new LinkedList<>(hm.entrySet());

        //  对List按entry的value排序(降序)
        list.sort((o1, o2) -> (o2.getValue()).compareTo(o1.getValue()));

        // 将排序后的元素放到LinkedHashMap中
        HashMap<String, Integer> temp = new LinkedHashMap<>();
        for (Map.Entry<String, Integer> aa : list) {
            temp.put(aa.getKey(), aa.getValue());
        }
        return temp;
    }
}
